package com.cloudkinto.controller.replacement;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.finance.vo.CostRecordListRes;
import com.cloudkinto.service.orderoutput.vo.OrderOutputExportReq;
import com.cloudkinto.service.replacement.ReplacementService;
import com.cloudkinto.service.replacement.vo.*;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanp
 * @version v1.0.0
 * @Description :
 * @Create on : 2021/4/13 17:00
 **/
@RestController
@Api(tags = "补发")
@RequestMapping("/api/admin/replacement")
public class ReplacementController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReplacementController.class);

    @Autowired
    ReplacementService service;


    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "endDate", value = "结束时间"),
            @ApiImplicitParam(name = "shopId", value = "店铺id"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "status", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = CostRecordListRes.class)})
    public PageResult listPage(String pageSize, String current, String status, String keyword, String shopId, String storageId, String startDate, String endDate) {
        Map<String, String> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("companyId", shopId);
        map.put("outputStorageId", storageId);
        map.put("startDate", startDate);
        map.put("status", status);
        map.put("endDate", endDate);
        if (getCompanyId() != null)
            map.put("companyId", getCompanyId() + "");
        return service.getListPage(map);
    }

    @UserSessionRequired
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody ReplacementUpdateReq replacementUpdateReq) {
        return service.updateR(getCurrentUserId(), replacementUpdateReq);
    }

    @UserSessionRequired
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除", notes = "删除")
    public SingleResult delete(@PathVariable("id") @Validated Long id) {
        Long currentUserId = getCurrentUserId();
        return service.delete(currentUserId, id);
    }

    //将主表中的某些信息带过来
    @UserSessionRequired
    @GetMapping("/addInit/{outputId}")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit(@PathVariable("outputId") Long outputId) {
        return service.addInit(outputId);
    }

    @UserSessionRequired
    @PostMapping("/add")
    @ApiOperation(value = "添加", notes = "添加")
    public SingleResult add(@RequestBody ReplacementAddReq replacementAddReq) {
        Long currentUserId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.add(currentUserId, replacementAddReq, companyId);
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCompanyId(), getCurrentUserId());
    }

    @UserSessionRequired
    @GetMapping("/getSkuList/{companyId}")
    @ApiOperation(value = "得到当前公司的sku", notes = "得到当前公司的sku")
    public SingleResult getSkuList(@PathVariable("companyId") Long companyId) {
        return service.getSkuList(companyId, getCurrentUserId());
    }

    @UserSessionRequired
    @PostMapping("/exportMessage")
    @ApiOperation(value = "打印配件信息", notes = "打印配件信息")
    public void exportMessage(@RequestBody ReplacementExportReq req, HttpServletResponse response) {
        service.exportMessage(req, response);
    }


    @UserSessionRequired
    @PostMapping("/checkMessage")
    @ApiOperation(value = "判断是否有同一订单下其他商品未选择")
    public SingleResult checkMessage(@RequestBody ReplacementSendEmailReq ids) {
        return service.checkMessage(ids);
    }

    @UserSessionRequired
    @PostMapping("/sendEmail")
    @ApiOperation(value = "向合作仓库发邮件")
    public SingleResult sendEmail(@RequestBody ReplacementSendEmailReq replacementSendEmail) throws Exception {
        return service.sendEmail(replacementSendEmail, getCurrentUserId());
    }

    @UserSessionRequired
    @PostMapping("updateOrderWaitSend")
    @ApiOperation(value = "更改订单为已打单状态")
    public SingleResult updateOrderWaitSend(@RequestBody OrderOutputExportReq req) throws Exception {
        Long companyId = getCompanyId();
        return service.updateOrderWaitSend(req.ids, getCurrentUserId());
    }

    @UserSessionRequired
    @PostMapping("/truck")
    @ApiOperation(value = "快递单号", notes = "快递单号")
    public SingleResult truck(@RequestBody ReplacementTruckReq replacementTruckReq) {
        Long currentUserId = getCurrentUserId();
        return service.truck(replacementTruckReq, currentUserId);
    }

    //打印拣货单
    @UserSessionRequired
    @PostMapping("/exportExcel")
    @ApiOperation(value = "打印拣货单", notes = "打印拣货单")
    public void exportAll(@RequestBody OrderOutputExportReq req, HttpServletResponse response) throws IOException {
        service.exportExcel(req.getIds(), response);
    }

    @UserSessionRequired
    @PostMapping("exportTrackExpress")
    @ApiOperation(value = "导出快递公司面单")
    public void exportTrackExpress(@RequestBody OrderOutputExportReq req, HttpServletResponse response) throws Exception {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        map.put("keyword", req.keyword);
        map.put("current", req.current);
        map.put("pageSize", req.pageSize);
        map.put("status", req.status);
        map.put("multi", req.multi);
        if (StringUtils.isNotBlank(req.getStartDate())) {
            map.put("startDate", req.getStartDate());
        }
        if (StringUtils.isNotBlank(req.getEndDate())) {
            map.put("endDate", req.getEndDate());
        }
        map.put("trackId", req.trackId);
        map.put("shopId", req.shopId);
        if (StringUtils.isNotBlank(req.getStorageId())) {
            map.put("storageId", req.getStorageId());
        }
        if (StringUtils.isNotBlank(req.getStorageId())) {
            map.put("storageId", req.getStorageId());
        } else {
            map.put("storageId", 0 + "");
        }
//        map.put("orderType", "1");//一件代发
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        service.exportTrackExpress(map, req.ids, response);
    }
    //    @UserSessionRequired
    @PostMapping("/uploadTrackNumberExcel")
    @ApiOperation(value = "导入快递单Excel", notes = "导入快递单Excel")
//    @ApiImplicitParams({@ApiImplicitParam(name = "file",value = "导入文件"  ,paramType = "file"), @ApiImplicitParam(name = "shopId", value = "店铺id"), @ApiImplicitParam(name = "storageId", value = "仓库id")})
    public SingleResult uploadTrackNumberExcel(MultipartFile file) {
        Long userid = getCurrentUserId();
        Map data = service.dealwithTrackNumberExcel(file, userid);
        return SingleResult.success(SysConstant.Order_Import_Success, data, data.get("count"));
    }

}
